package com.qf.android.huihui.view.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.qf.android.huihui.R;
import com.qf.android.huihui.adapter.information.DbSearchAdapter;
import com.qf.android.huihui.adapter.information.SearchGridViewAdapter;
import com.qf.android.huihui.bean.SearchBean;
import com.qf.android.huihui.bean.dbbean.DBSearchBean;
import com.qf.android.huihui.presenter.SearchPresenter;
import com.qf.android.huihui.view.activity.common.BaseActivity;
import com.qf.android.huihui.view.activity.impl.ISearchView;

import java.util.List;

public class SearchActivity extends BaseActivity implements ISearchView, AdapterView.OnItemClickListener {
    private GridView gv;
    private SearchPresenter presenter;
    private SearchGridViewAdapter adapter;
    private EditText et;
    private SearchBean sb;
    private ListView lv;
    private DbUtils db;
    private List<DBSearchBean> list;
    private DbSearchAdapter adapter1;
    private Selector s;
    private TextView tv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initWindow();
        init();
    }

    private void init() {

        db = DbUtils.create(this);
        lv = (ListView) findViewById(R.id.search_lv);
        gv = (GridView) findViewById(R.id.search_gridView);
        et = (EditText) findViewById(R.id.search_et);
        presenter = new SearchPresenter(this);
        //添加足视图
        tv = new TextView(this);
        tv.setText("清空浏览记录");
        tv.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT));
        tv.setGravity(Gravity.CENTER);

        lv.addFooterView(tv);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(SearchActivity.this, EmptyActivity.class);
                intent.putExtra("content", list.get(i).getName());
                startActivity(intent);
            }
        });
        presenter.getData();
        s = Selector.from(DBSearchBean.class);
        initDb();
        lv.setAdapter(adapter1);
        gv.setOnItemClickListener(this);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    db.deleteAll(DBSearchBean.class);
                    initDb();
                } catch (DbException e) {
                    e.printStackTrace();
                }
            }
        });
        et.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    //进行搜索操作的方法，在该方法中可以加入mEditSearchUser的非空判断
                    if (TextUtils.isEmpty(et.getText().toString().trim())) {
                        Toast.makeText(SearchActivity.this, "数据为空", Toast.LENGTH_SHORT).show();
                    } else {
                        DBSearchBean b = new DBSearchBean();
                        b.setName(et.getText().toString().trim());

                        try {
                            db.save(b);
                            initDb();
                        } catch (DbException e) {
                            e.printStackTrace();
                        }


                        //   Log.e("Ts","Aaaaaaaaa11");

                        Intent intent = new Intent(SearchActivity.this, EmptyActivity.class);
                        intent.putExtra("content", et.getText().toString().trim());
                        startActivity(intent);
                    }

                }
                return false;
            }
        });

    }

    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    public void backClick(View view) {
        finish();
    }

    @Override
    public void setDate(SearchBean bean) {
        sb = bean;
        adapter = new SearchGridViewAdapter(bean, this);
        gv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        et.setText(sb.getData().get(i).getName());

    }

    private void initDb() {
        s = Selector.from(DBSearchBean.class);
        try {
            list = db.findAll(s);
        } catch (DbException e) {
            e.printStackTrace();
        }
        adapter1 = new DbSearchAdapter(list, this);
        adapter1.notifyDataSetChanged();
    }
}
